﻿using System.Text;
using BO.Infrastructure.Utils;
using Utilities.Entities;
using System;
using BugManager;
using BO.News;
using GUI.Extensions;
using System.Collections.Generic;

namespace GUI.Controls.Embed
{
    public partial class EmbedTemplate6 : System.Web.UI.UserControl
    {
        private const string LinkFormat = "<a href=\"{0}\" title=\"{2}\">{1}</a>";
        private const int ImageWidth = 240;
        private const int ImageHeight = 180;

        internal bool DataBind(CategoryObject category, List<NewsObject> data, bool displayRelateNews = true)
        {
            try
            {
                if (category != null)
                {
                    ltrBoxTitle.Text = string.Format(LinkFormat, category.Url, category.Name,
                                                     PresentationUtils.EnCode(category.Name));
                }

                if (data != null && data.Count > 0)
                {

                    var sb = new StringBuilder();
                    foreach (var news in data)
                    {
                        var relateNews = string.Empty;
                        if (displayRelateNews && news.RelateNews != null && news.RelateNews.Length > 0)
                            relateNews = BuildRelateNews(news.RelateNews);
                        var avatar = PresentationUtils.Photo.BuildPhoto(news.Avatar, news.Title, news.Url, ImageWidth,
                                                                        ImageHeight, lazyLoad: true);
                        sb.AppendFormat(
                            "<li>{3}<div class=\"scope clearfix\"><h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2>{4}</div></li>",
                            news.Url,
                            news.Title,
                            PresentationUtils.EnCode(news.Title),
                            avatar,
                            relateNews);
                    }
                    ltrData.Text = sb.ToString();
                }

                return true;
            }
            catch (Exception ex)
            {
                ErrorManager.Log(ex.ToString());
                return false;
            }

        }

        public static string BuildRelateNews(NewsObject[] data)
        {
            if (data != null && data.Length > 0)
            {
                var sb = new StringBuilder();
                foreach (var news in data)
                {
                    sb.AppendFormat(
                        "<li><a href=\"{0}\" title=\"{2}\">{1}</a></li>",
                        news.Url,
                        news.Title,
                        PresentationUtils.EnCode(news.Title));
                }
                return string.Format("<ul class=\"relation_news fr\">{0}</ul>", sb);
            }
            return string.Empty;
        }

        internal void DataBind(int zoneId)
        {
            if (zoneId > 0)
            {
                var category = NewsBO.GetInstance().GetCategoryDetail(zoneId);
                if (category != null)
                {
                    var bo = CacheSettingExtensions.EnableCacheHtml
                                 ? NewsBO_NoCache.GetInstance()
                                 : NewsBO.GetInstance();
                    var data = bo.ImportNewsWhileNotEnough(zoneId, 2, 2);
                    DataBind(category, data);
                }
            }
        }
    }
}